package com.ruoyi.project.module.zxm.tool;

import com.ruoyi.common.redis.service.RedisService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Aspect
@Component
public class TimeLogAspect {

    @Resource
    private RedisService redisService;

    private static final Logger logger = LoggerFactory.getLogger(TimeLogAspect.class);
    @Around("@annotation(com.ruoyi.project.module.zxm.tool.TimeLog)")

    public Object timeCost(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long endTime = System.currentTimeMillis();
        logger.info("方法[{}]耗时[{}]ms", joinPoint.getSignature().toShortString(), endTime - startTime);
        String methodName = joinPoint.getSignature().toShortString();
        String timeInfo = methodName + "耗时:" + (endTime - startTime) + "ms";
        String topLevelKey = "methods";
        String subKey = joinPoint.getSignature().toShortString().replace(".", ":");

        String fullKey = topLevelKey + ":" + subKey;
        System.out.println(subKey+"woshi");
        // 存储值到 Redis
        redisService.setCacheObject(fullKey, timeInfo);
        return result;
    }
}
